#ifndef __TTCODE_STATE_CHART_H__
#define __TTCODE_STATE_CHART_H__

#include "comLogManager.h"


#include <boost/statechart/state_machine.hpp>
#include <boost/statechart/simple_state.hpp>
#include <boost/statechart/event.hpp>
#include <boost/statechart/simple_state.hpp>
#include <boost/statechart/transition.hpp>
#include <boost/statechart/in_state_reaction.hpp>
#include <boost/mpl/list.hpp>


#include <map>
#include <string>
extern std::map<std::string,std::string> globalTTEnv;


extern std::map<std::string,std::string> globalFileType;



#include "MainFrameAui.h"
extern MainFrameAui *globalAui;
#include "MainFrameMgr.h"
extern MainFrameMgr *globalFrame;
#include "ProjectNaviMgr.h"
extern ProjectNaviMgr *globalNavi;
#include "TextEditMgr.h"
extern TextEditMgr *globalTextEdit;
#include "InfoSpaceMgr.h"
extern InfoSpaceMgr *globalInfo;


#include "ProjectDocMgr.h"
extern ProjectDocMgr *globalDocMgr;

#include "IconImageMgr.h"
extern IconImageMgr* globalIiMgr;

#include "TemplateMgr.h"
extern TemplateMgr* globalTempMgr;

#include "InfoCollectMgr.h"
extern InfoCollectMgr* globalCollectMgr;
















// states
struct stateAppStart;
struct stateEmpty;
struct stateProjectOpen;

//! events
struct eventInitWidgets : boost::statechart::event< eventInitWidgets > {};
// empty project
struct eventEmpty : boost::statechart::event< eventEmpty > {};
// new project
struct eventNewProject : boost::statechart::event<eventNewProject>
{
	string name;
	string home;
};
// load project
struct eventLoadProject : boost::statechart::event<eventLoadProject> {};
// finnish load project
struct evendLoadFinnish : boost::statechart::event<evendLoadFinnish> {};
// close project
struct eventCloseProject : boost::statechart::event<eventCloseProject> {};
// finnish close project
struct eventCloseFinnish : boost::statechart::event<eventCloseFinnish> {};


// build
struct eventBuild : boost::statechart::event<eventBuild>
{
	string param;
};
struct eventClean : boost::statechart::event<eventClean>
{
	string param;
};
struct eventCancel : boost::statechart::event<eventCancel> {};

// FillReport
struct eventFillReportDirect : boost::statechart::event<eventFillReportDirect>
{
	string filepath;
	string pos;
	string content;
	string tips;
};

struct eventCloseAll : boost::statechart::event<eventCloseAll> {};



struct ttcodeMainCtrlStateChart : boost::statechart::state_machine<ttcodeMainCtrlStateChart, stateAppStart>{};




struct stateAppStart : boost::statechart::simple_state<stateAppStart, ttcodeMainCtrlStateChart>
{
	void onInitWidgets( const eventInitWidgets & );
	void onLoadProject( const eventLoadProject & );

	typedef boost::mpl::list<
		boost::statechart::in_state_reaction< eventInitWidgets, stateAppStart, &stateAppStart::onInitWidgets >,
		boost::statechart::transition < eventEmpty, stateEmpty >,
		boost::statechart::in_state_reaction< eventLoadProject, stateAppStart, &stateAppStart::onLoadProject >,
		boost::statechart::transition < evendLoadFinnish, stateProjectOpen>
	> reactions;
};

struct stateEmpty : boost::statechart::simple_state<stateEmpty, ttcodeMainCtrlStateChart>
{
	void onNewProject( const eventNewProject & evt);

	typedef boost::mpl::list<
		boost::statechart::in_state_reaction< eventNewProject, stateEmpty, &stateEmpty::onNewProject >,
		boost::statechart::transition < evendLoadFinnish, stateProjectOpen>
	> reactions;
};

struct stateProjectOpen : boost::statechart::simple_state<stateProjectOpen, ttcodeMainCtrlStateChart>
{
	void onCloseProject( const eventCloseProject & evt);
	void onNewProject( const eventNewProject & evt);

	void onBuild( const eventBuild & evt);
	void onClean( const eventClean & evt);

	void onFillReportDirect( const eventFillReportDirect & evt);

	void onCloseAll( const eventCloseAll & evt);

	typedef boost::mpl::list<
		boost::statechart::in_state_reaction< eventCloseProject, stateProjectOpen, &stateProjectOpen::onCloseProject >,
		boost::statechart::in_state_reaction< eventNewProject, stateProjectOpen, &stateProjectOpen::onNewProject >,
		
		boost::statechart::in_state_reaction< eventBuild, stateProjectOpen, &stateProjectOpen::onBuild >,
		boost::statechart::in_state_reaction< eventClean, stateProjectOpen, &stateProjectOpen::onClean >,
		
		boost::statechart::in_state_reaction< eventFillReportDirect, stateProjectOpen, &stateProjectOpen::onFillReportDirect >,

		boost::statechart::in_state_reaction< eventCloseAll, stateProjectOpen, &stateProjectOpen::onCloseAll >,

		boost::statechart::transition < evendLoadFinnish, stateProjectOpen >,
		boost::statechart::transition < eventCloseFinnish, stateEmpty >
	> reactions;
};



template<class T>
void DisplayStateConfiguration( const T & sm )
{
    for (
         typename T::state_iterator pLeafState = sm.state_begin();
         pLeafState != sm.state_end(); ++pLeafState )
    {
        ttcode_log << typeid( *pLeafState ).name() << "\n";
    }
}

extern ttcodeMainCtrlStateChart globalStateChart;

inline int AutoGenID(void)
{
	static int id = 10000; 
	return id++;
};

#endif // __TTCODE_STATE_CHART_H__